async function renderclass(data){
    
    await rendertable(data=data,domhtml='.classinfo tbody',domdata=['id','classname','timestamp','major'])
 
    dataform['classinfodata']=data
    dataform['classinfodata1']=data
    for (const i in data){
        $(`.classinfo tbody tr:eq(${i})`).append(`<td><button class="layui-btn" id=${data[i]['id']} onclick="delclassinfo(this)">删除</button><button class="layui-btn layui-bg-blue" id=${data[i]['id']} onclick="upclassinfo(this)">编辑</button></td>`)
    
}

}
!(async function(){
    response=await taskObject.fetchtemplate(path='/api/admin/classinfo')
    if(response.code=200){
        await renderclass(data=response.data)
    }
    $('#classbtn').on('click',async (even)=>{
            even.preventDefault()
           let classname=$('#classname').val()
           response=await taskObject.fetchtemplate(path='/api/admin/classinfo?classname='+classname,method='GET')
           if(response.code==200){
            await renderclass(data=response.data)
           }
        })
 

    //添加班级 teacherdata

  $('.classinfo i').on('click',async ()=>{
      await openCollegeMajorClassForm()
  })

}())

async function openCollegeMajorClassForm() {
    layui.use(['layer', 'form', 'jquery'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var $ = layui.jquery;

        let collerhtml = '';
        let collegenamedata=[]
        for (const i of dataform['majordata']) {
            collegenamedata.push(i['collegename'])
            
        }
        for (const key of Array.from(new Set(collegenamedata))){
            collerhtml += `<option value="${key}">${key}</option>`;
        }

        var formHtml = `
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">学院</label>
                    <div class="layui-input-block">
                        <select name="college" lay-verify="required" id="collegeselect">
                            <option value="">请选择学院</option>` + collerhtml + `</select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">专业名称</label>
                    <div class="layui-input-block">
                        <select name="major" lay-verify="required" class="majorlist">
                            <option value="">请选择专业</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">输入班级</label>
                    <div class="layui-input-block">
                        <input type="text" name="class" required lay-verify="required" placeholder="班级" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="formDemo">添加</button>
                        <button type="reset" class="layui-btn layui-btn-primary">取消</button>
                    </div>
                </div>
            </form>
        `;

        layer.open({
            type: 1,
            title: '添加班级',
            area: ['500px', '400px'],
            content: formHtml,
            success: function(layero, index){
              
               
               
                form.on('select', function(data){
                    elemid=data.elem.id
                    
                    if(elemid=='collegeselect'){
                         
                        let majorlisthtml='<option value="">请选择专业</option>'
                        let collegename=$('#collegeselect').val()
                        for (const key of dataform.majordata){
                           
                            if (key.collegename==collegename){
                                majorlisthtml+=`<option value="${key.major}">${key.major}</option>`
                            }

                                }
                       
                        $('.majorlist').html(majorlisthtml)
                        form.render()
                    }
                    
                });
                form.on('submit(formDemo)', async function(data){
                    console.log(data);
                    let requestdata=data.field
                    requestdata['classname']=requestdata['class']
                    requestdata['collegename']=requestdata['college']
                    response=await taskObject.fetchtemplate(path='/api/admin/classinfo',method='POST',data=requestdata)
                     if(response.code!=200){
                        layer.msg(response['message'],{icon:2,time:2000})
                        layer.close(index);
                        return ;
                     }else{
                     setTimeout(()=>{
                        console.log(response);
                        layer.msg('添加班级成功',{icon:1,time:2000})
                        setTimeout(()=>{
                            window.location.reload()
                        },1000)
                        layer.close(index)
                     },1500)

                    return false; }

                });
                $('body').on('submit',(even)=>{
                    even.preventDefault();
                })

                form.render(); // Render the form fields
            }
        });
    });
}

async function upclassinfo(even){
  
       valueid=parseInt(even.id)
       layer.prompt({title: '请输入班级名称'}, async function(value, index, elem){
        if(value === '') return elem.focus();
        let major=null
        let number=0
         for (const key of dataform.classinfodata){
            if(key.id==valueid){
                major=key.major

                break;
            }
            number+=1
         }
         let collegename=null
         for (const i of dataform.majordata){
               if(major==i.major){
                collegename=i.collegename
                break;
               }
         }
         response=await taskObject.fetchtemplate(path='/api/admin/classinfo',method='PUT',data={'collegename':collegename,'classname':value,'id':valueid})
         if(response.code==200){
             setTimeout(()=>{layer.msg('编辑数据成功',{icon:1,time:1500})})
             
             setTimeout(async ()=>{
                dataform.classinfodata[number]['classname']=value
             await renderclass(data=dataform.classinfodata)
             },2000)
         }else{
            layer.msg(response['message'],{icon:2,time:1500})
         }
        layer.close(index);
      });
           
}

async function delclassinfo(even){
    valueid=parseInt(even.id)
    response=await taskObject.fetchtemplate(path='/api/admin/classinfo?id='+valueid,method='DELETE',data={})
    if (response.code==200){

        layer.msg('删除数据成功',{icon:1,time:1500})
        index=0
        for (const key of dataform.classinfodata){
            if(key.id==valueid){
                console.log(dataform);
                dataform.classinfodata[index]=null
                break;

            }
            index+=1
        }
        setTimeout(async ()=>{
            await renderclass(data=dataform.classinfodata)
        },1500)
       
        
        return ;
    }else{
        layer.msg(response['message'],{icon:2,time:1500})
    }
}